#include "timeInterval_JY.h"
#include <stdlib.h>
#include <stdio.h>

// interval Time
#if defined(__linux__) || defined(__unix__)
struct timeval static timeMark = {.tv_sec = 0, .tv_usec = 0};
double countTimeInterval_JY()
{
    if (timeMark.tv_sec == 0 || timeMark.tv_usec == 0)
    {
        gettimeofday(&timeMark, NULL);
        return 0.0;
    }
    else
    {
        struct timeval now;
        gettimeofday(&now, NULL);
        double timediff = ((now.tv_sec - timeMark.tv_sec) * 1000 * 1000) + (now.tv_usec - timeMark.tv_usec);
        gettimeofday(&timeMark, NULL);
        return timediff * 0.001;
    }
}
#elif _WIN32
LARGE_INTEGER static timeMark, cpuFrequency;
double countTimeInterval_JY()
{
    if (timeMark.QuadPart == 0 || cpuFrequency.QuadPart == 0)
    {
        QueryPerformanceCounter(&timeMark);
        QueryPerformanceFrequency(&cpuFrequency);
        return 0;
    }
    else
    {
        LARGE_INTEGER now;
        QueryPerformanceCounter(&now);
        double cost = (double)(now.QuadPart - timeMark.QuadPart) * 1000 / (double)cpuFrequency.QuadPart;
        QueryPerformanceCounter(&timeMark);
        return cost;
    }
}
#endif
